package com.simba.server;

import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import com.simba.GlobalParam;
import com.simba.common.DefaultServerInfo;
import com.simba.common.log.LogHelper;
import com.simba.server.components.SorterParams;

public class ServerParam
{
	public static String ACCESS_LOG = "access.log";
	public static String CONFIG_XML_FILE = "etc" + GlobalParam.PATH_SEP + "simba-server-config.xml";
	public static int DECODER_THREAD_POOL_SIZE = 10;
	public static String ERROR_LOG = "error.log";
	public static int EVENT_THREAD_POOL_SIZE = 5;
	public static final String FALSE_STRING = "false";
	public static int IO_THREAD_POOL_SIZE = 5;
	public static boolean IS_WINDOWS_SERVER = false;
	public static String LOG_PATH = "logs";
	public static String NEED_DEBUG = ServerParam.FALSE_STRING;
	public static String REMOTE_WEBSERVICE_ADDRESS = "http://127.0.0.1:8080/pcs-tc-nc-job/WyService/services/CommWY?wsdl";
	public static String TOKEN = "";

	public static String VERBOSE_LOG = "verbose.log";
	private static DefaultServerInfo serverInfo;

	public static DefaultServerInfo getServerInfo()
	{
		return ServerParam.serverInfo;
	}

	public static void initGlobalConfiguration()
	{
		try
		{
			String os = System.getProperty("os.name");
			ServerParam.IS_WINDOWS_SERVER = os.substring(0, 3).equalsIgnoreCase("win");
			ServerParam.serverInfo = new DefaultServerInfo();

			Document rootMessage = ServerManager.getInstance()
					.getXmlDocFromFile(ServerParam.CONFIG_XML_FILE);
			Element rootElement = rootMessage.getRootElement();

			ServerParam.initConnectionSectionConfiguration(rootElement);
			ServerParam.initGlobalSectionConfiguration(rootElement);
		}
		catch (Exception ex)
		{
			System.out.println("Init global server configuration " + ServerParam.CONFIG_XML_FILE
					+ " error: " + ex);
			System.out.println("Server Started failed");
			System.exit(5000);
		}
	}

	public static void initRootLogger(String configFile)
	{
		File fp = new File(ServerParam.LOG_PATH);
		if (fp.exists())
		{
			boolean canWrite = fp.canWrite();
			if (!canWrite)
			{
				System.out.println("Fatal Error: Log path: " + ServerParam.LOG_PATH + " can't write");
				System.exit(5000);
			}
		}
		else
		{
			if (!fp.mkdir())
			{
				System.out.println("Fatal Error: Data path: " + ServerParam.LOG_PATH + " can't create");
				System.exit(5000);
			}
		}
		String accessLogPath = ServerParam.LOG_PATH + GlobalParam.PATH_SEP + ServerParam.ACCESS_LOG;
		String errorLogPath = ServerParam.LOG_PATH + GlobalParam.PATH_SEP + ServerParam.ERROR_LOG;
		String verboseLogPath = ServerParam.LOG_PATH + GlobalParam.PATH_SEP
				+ ServerParam.VERBOSE_LOG;
		try
		{
			LogHelper.configure(configFile, verboseLogPath, accessLogPath, errorLogPath);
		}
		catch (Exception e)
		{
		}
	}

	public static void printStartedInfo()
	{
		System.out.println("Simba Server has been Started!");
		System.out.println("*****************************************");
		System.out.println("*                                       *");
		System.out.println("*                                       *");
		System.out.println("*         simba-post-wheel-server       *");
		System.out.println("*                                       *");
		System.out.println("*                                       *");
		System.out.println("*****************************************");
		System.out.println(" ");
	}

	public static String replaceSlash(String path)
	{
		String result = path;
		if (GlobalParam.PATH_SEP.equals("\\"))
		{
			result = result.replaceAll("/", "\\\\");
		}
		else
		{
			result = result.replaceAll("\\\\", "/");
		}
		if (result.substring(result.length() - 1).equals(GlobalParam.PATH_SEP))
		{
			result = result.substring(0, result.length() - 1);
		}
		return result;
	}

	private static void initConnectionSectionConfiguration(Element rootElement)
	{
		Element connElement = rootElement.getChild("Connection");

		String frameServerAddress = connElement.getChild("frame-sorter-server").getAttribute("ip")
				.getValue();
		ServerParam.serverInfo.setDefaultServerAddress(frameServerAddress);
		ServerParam.serverInfo.setDefaultServerPort(Integer.parseInt(
				connElement.getChild("frame-sorter-server").getAttribute("port").getValue()));

		String jsonServerAddress = connElement.getChild("json-content-server").getAttribute("ip")
				.getValue();
		ServerParam.serverInfo.setJsonServerAddress(jsonServerAddress);
		ServerParam.serverInfo.setJsonServerPort(Integer.parseInt(
				connElement.getChild("json-content-server").getAttribute("port").getValue()));

		String httpServerAddress = connElement.getChild("http-json-server").getAttribute("ip")
				.getValue();
		ServerParam.serverInfo.setHttpServerAddress(httpServerAddress);
		ServerParam.serverInfo.setHttpServerPort(Integer.parseInt(
				connElement.getChild("http-json-server").getAttribute("port").getValue()));

		ServerParam.REMOTE_WEBSERVICE_ADDRESS = ""
				+ connElement.getChild("remote-webservice").getAttributeValue("path");

		ServerParam.TOKEN = connElement.getChild("remote-webservice").getAttributeValue("token");
	}

	private static void initGlobalSectionConfiguration(Element rootElement) throws Exception
	{
		Element globalElement = rootElement.getChild("Global");

		ServerParam.EVENT_THREAD_POOL_SIZE = Integer
				.parseInt(globalElement.getChild("event-threads").getText());
		ServerParam.IO_THREAD_POOL_SIZE = Integer
				.parseInt(globalElement.getChild("io-workers").getText());
		ServerParam.DECODER_THREAD_POOL_SIZE = Integer
				.parseInt(globalElement.getChild("decoder-workers").getText());

		ServerParam.LOG_PATH = globalElement.getChild("log-path").getText();
		ServerParam.NEED_DEBUG = globalElement.getChild("need-debug").getText();

		String newThreadEachReq = globalElement.getChild("new-thread-each-request").getText();
		SorterParams.getInstance().setNewThreadEachRequest(newThreadEachReq.equalsIgnoreCase("true"));

		SorterParams.getInstance().setSorterType(Integer.parseInt(globalElement.getChild("sort-type").getText()));

		String repeatUpload = globalElement.getChild("repeat-upload").getText();
		SorterParams.getInstance().setRepeatUpload(Boolean.parseBoolean(repeatUpload));

		String allowChange = globalElement.getChild("allow-change-remote").getText();
		SorterParams.getInstance().setAllowChange(Boolean.parseBoolean(allowChange));
	}
}
